#ifndef FHE_PRIMS_ISPATIALNODE_H
#define FHE_PRIMS_ISPATIALNODE_H

#include <fhe/math/Mat.h>
#include <fhe/core/Node.h>

namespace fhe
{
  namespace core
  {
    template <size_t D, typename T>
    class SpatialNode : public Node
    {
    public:
      typedef Mat<D,T> M;
      typedef typename M::V V;
      typedef typename M::R R;
      
      SpatialNode()
        : scale( V::ONE )
      {
      }
      
      virtual ~SpatialNode()
      {
      }
      
      V position;
      R rotation;
      V scale;
      
      static std::string typeName()
      {
        std::ostringstream os;
        os << "SpatialNode";
        os << D;
        os << typeid(T).name();
        return os.str();
      }
      
      M localTransform() const
      {
        return M::translation( position ) * M::rotation( rotation ) * M::scale( scale );
      }
      
      M globalTransform() const
      {
        M m;
        for ( Ptr node = const_cast<SpatialNode*>(this); node; node = node->parent() )
        {
          if ( node->is( typeName() ) )
          {
            m = node->get<M>( "localTransform" ) * m;
          }
        }
        return m;
      }
    };
    
    typedef SpatialNode<2,int> SpatialNode2i;
    typedef SpatialNode<2,double> SpatialNode2d;
    typedef SpatialNode<3,int> SpatialNode3i;
    typedef SpatialNode<3,double> SpatialNode3d;
  }
}

#endif
